//
//  GFVOParser.m
//  GolfFriend
//
//  Created by Vescky on 13-11-26.
//  Copyright (c) 2013年 vescky.org. All rights reserved.
//

#import "GFVOParser.h"
#import "JSON.h"
#import "RegexKitLite.h"

#import "CommonUtility.h"
#import "DateUtility.h"

@implementation GFVOParser

GFVOParser *voParser;

+ (id)shareParser {
    if (!voParser) {
        voParser = [[GFVOParser alloc] init];
    }
    return voParser;
}

- (UserPrivateInfoVO*)parseDataForUserPrivateInfo:(NSDictionary*)dataDict {
    UserPrivateInfoVO *upInfo = [[UserPrivateInfoVO alloc] init];
    if (dataDict) {
        NSDictionary *pDict = [dataDict objectForKey:@"privateInfo"];
        if (isValueSet(pDict)) {
            upInfo.userId = [pDict objectForKey:@"id"];
            upInfo.nickName = [pDict objectForKey:@"nickName"];
            upInfo.password = [pDict objectForKey:@"password"];
            upInfo.phone = [pDict objectForKey:@"phone"];
            upInfo.trueName = [pDict objectForKey:@"trueName"];
            upInfo.token = [pDict objectForKey:@"token"];
            upInfo.resgTime = [pDict objectForKey:@"resgTime"];
            upInfo.friendList = [pDict objectForKey:@"friendList"];
            upInfo.blackList = [pDict objectForKey:@"blackList"];
        }
    }
    return upInfo;
}

- (UserPublicInfoVO*)parseDataForUserPublicInfo:(NSDictionary*)dataDict {
    UserPublicInfoVO *upbInfo = [[UserPublicInfoVO alloc] init];
    if (dataDict) {
        NSDictionary *pDict = [dataDict objectForKey:@"publicInfo"];
        if (isValueSet(pDict)) {
            upbInfo.recordId = [pDict objectForKey:@"id"];
            upbInfo.userId = [pDict objectForKey:@"uId"];
            upbInfo.sex = [pDict objectForKey:@"sex"];
            upbInfo.avatar = [pDict objectForKey:@"avatar"];
            upbInfo.motto = [pDict objectForKey:@"motto"];
            upbInfo.area = [pDict objectForKey:@"area"];
            upbInfo.lastLoginTime = [pDict objectForKey:@"lastLoginTime"];
            upbInfo.lastLoginProvince = [pDict objectForKey:@"lastLoginProvince"];
            upbInfo.lastLoginCity = [pDict objectForKey:@"lastLoginCity"];
            upbInfo.lastLoginPosition = [pDict objectForKey:@"lastLoginPosition"];
            upbInfo.lastLoginToken = [pDict objectForKey:@"lastLoginToken"];
            upbInfo.teams = [pDict objectForKey:@"teams"];
            upbInfo.enName = [pDict objectForKey:@"enName"];
            upbInfo.teamName = [pDict objectForKey:@"teamName"];
            upbInfo.teamLogo = [pDict objectForKey:@"teamLogo"];
            upbInfo.nickName = [pDict objectForKey:@"nickName"];
            if ([[pDict objectForKey:@"prestige"] respondsToSelector:@selector(integerValue)]) {
                upbInfo.prestige = [[pDict objectForKey:@"prestige"] integerValue];
            }
            if ([[pDict objectForKey:@"positionFactor"] respondsToSelector:@selector(floatValue)]) {
                upbInfo.positionFactor = [[pDict objectForKey:@"positionFactor"] floatValue];
            }
            if ([[pDict objectForKey:@"lastLoginLatitude"] respondsToSelector:@selector(floatValue)]) {
                upbInfo.lastLoginLatitude = [[pDict objectForKey:@"lastLoginLatitude"] floatValue];
            }
            if ([[pDict objectForKey:@"lastLoginLongitude"] respondsToSelector:@selector(floatValue)]) {
                upbInfo.lastLoginLongitude = [[pDict objectForKey:@"lastLoginLongitude"] floatValue];
            }
        }
    }
    return upbInfo;
}

- (NSMutableArray*)parseDataForChatMessage:(NSDictionary*)dataDict {
    NSArray *cArray = [dataDict objectForKey:@"content"];
    if (cArray.description.length <= 0 || ![cArray respondsToSelector:@selector(count)]) {
        return nil;
    }
   
    if (isValueSet(cArray)) {
        NSMutableArray *resultArr = [[NSMutableArray alloc] init];
        for (int i = 0; i < [cArray count]; i++) {
            NSDictionary *dict = [cArray objectAtIndex:i];
            ChatMessageVO *chatVo = [[ChatMessageVO alloc] init];
            chatVo.recordId = [dict objectForKey:@"id"];
            chatVo.fromId = [dict objectForKey:@"id"];
            chatVo.toId = [dict objectForKey:@"id"];
            chatVo.groupId = @"";
            chatVo.content = [dict objectForKey:@"content"];
            chatVo.time = getDateFromString(@"yyyy-MM-dd HH:mm:ss", [dict objectForKey:@"time"]);
            chatVo.sourceLink = [dict objectForKey:@"sourceLink"];
            chatVo.isGroup = 0;
            chatVo.isRead = [[dict objectForKey:@"sourceLink"] boolValue];
            chatVo.msgType = [dict objectForKey:@"msgType"];
            [resultArr addObject:chatVo];
        }
        return resultArr;
    }
    return nil;
}

- (TeamInfoVO*)parseDataForTeamInfo:(NSDictionary*)dataDict {
    return nil;
}

- (NSMutableArray*)paseDataForCircleContentArray:(NSDictionary*)dataDict {
    NSArray *cArray = [dataDict objectForKey:@"content"];
    if (cArray.description.length <= 0 || ![cArray respondsToSelector:@selector(count)]) {
        return nil;
    }
    NSMutableArray *resultArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < [cArray count]; i++) {
        NSDictionary *cDict = [cArray objectAtIndex:i];
        CircleContentVO *cVo = [[CircleContentVO alloc] init];
        cVo.articleId = [cDict objectForKey:@"id"];
        cVo.content = [cDict objectForKey:@"content"];
        cVo.userId = [cDict objectForKey:@"uId"];
        cVo.time = [cDict objectForKey:@"time"];
        cVo.clicks = [cDict objectForKey:@"clicks"];
        cVo.comments = [cDict objectForKey:@"comments"];
        cVo.zan = [cDict objectForKey:@"zan"];
        cVo.userName = [cDict objectForKey:@"userNickName"];
        cVo.userNickName = [cDict objectForKey:@"userNickName"];
        cVo.avatar = [cDict objectForKey:@"avatar"];
        cVo.cityName = [cDict objectForKey:@"location"];
        cVo.isSupported = [cDict objectForKey:@"isSupported"];
//        if (cVo.cityName.length <= 0) {
//            cVo.cityName = @"";
//        }
        NSMutableDictionary *lDict = [cDict objectForKey:@"sourceLinks"];
//        cVo.sourceLinks = [sLinks JSONValue];
        if ([lDict respondsToSelector:@selector(count)]&&isValueSet(lDict)) {
            cVo.sourceLinks = lDict;
        }
        [resultArr addObject:cVo];
    }
    if ([resultArr count] <= 0) {
        return nil;
    }
    return resultArr;
}

- (NSMutableArray*)parseDataForCircleCommentArray:(NSDictionary*)dataDict {
    return [NSMutableArray arrayWithArray:[dataDict objectForKey:@"list"]];
}

- (NSMutableArray*)paseDataForOfficalNewsContentArray:(NSDictionary*)dataDict {
    return [NSMutableArray arrayWithArray:[dataDict objectForKey:@"list"]];
}

- (NSMutableArray*)parseDataForOfficalNewsCommentArray:(NSDictionary*)dataDict {
    return [NSMutableArray arrayWithArray:[dataDict objectForKey:@"list"]];
}


- (WetherInfoVO*)parseDataForWetherInfo:(NSDictionary*)dataDict {
    return nil;
}

- (WetherInfoVO*)parseForBaiduWeatherForcast:(NSString*)wString {
    WetherInfoVO *wInfo = [[WetherInfoVO alloc] init];
    NSDictionary *dict = [wString JSONValue];
    if (!dict || [dict count] < 1) {
        return nil;
    }
    if (![[dict objectForKey:@"status"] isEqualToString:@"success"]) {
        return nil;
    }
    NSArray *weatherList = [dict objectForKey:@"results"];
    if (!weatherList || [weatherList count] < 1) {
        return nil;
    }
    NSDictionary *detailWeatherInfo = [weatherList objectAtIndex:0];
    if (!detailWeatherInfo) {
        return nil;
    }
    
    wInfo.cityName = [detailWeatherInfo objectForKey:@"currentCity"];
    wInfo.cityNameEn = @"";
    wInfo.cityId = @"";
    NSArray *arr = [detailWeatherInfo objectForKey:@"weather_data"];
    NSMutableArray *tempArr = [[NSMutableArray alloc] init];
    NSMutableArray *weatherArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < [arr count]; i++) {
        NSDictionary *dDict = [arr objectAtIndex:i];
        if (i == 0) {
            NSString *currentInfo = [dDict objectForKey:@"date"];
            NSString *currentTmp = [currentInfo stringByMatching:@"：.*℃"];
            currentTmp = [currentTmp stringByReplacingOccurrencesOfString:@"：" withString:@""];
            currentTmp = [currentTmp stringByReplacingOccurrencesOfString:@"℃" withString:@""];
            wInfo.currentTemp = currentTmp;
            wInfo.currentWeather = [dDict objectForKey:@"weather"];
            wInfo.currentWeatherIcon = getWeatherIcon(wInfo.currentWeather, YES);
        }
        
        if ([dDict objectForKey:@"weather"]) {
            NSString *w1 = [dDict objectForKey:@"weather"];
            [weatherArr addObject:simplifyWeather(w1)];
        }
        if ([dDict objectForKey:@"temperature"]) {
            NSString *t1 = [dDict objectForKey:@"temperature"];
            //            t1 = [t1 stringByReplacingOccurrencesOfRegex:@"\\u*" withString:@""];
            [tempArr addObject:t1];
        }
        
    }
    wInfo.sixDaysTemp = tempArr;
    wInfo.sixDaysWeather = weatherArr;
    return wInfo;
}


@end
